<template>
	<view class="con">
		<uni-forms ref="valiForm" :modelValue="userinfo" :rules="rules">
			<uni-forms-item label="用户名" required name="name">
				<uni-easyinput v-model="userinfo.name" placeholder="请输入用户名" />
			</uni-forms-item>
			<uni-forms-item label="密码" required name="password">
				<uni-easyinput v-model="userinfo.password" placeholder="请输入密码" type="password" />
			</uni-forms-item>
			<!-- <uni-forms-item label="头像" required name="headpic">
				<uni-file-picker v-model="headpic" file-mediatype="image" mode="grid" file-extname="png,jpg" :limit="1"
					@progress="progress" @success="success" @fail="fail" @select="select" />
			</uni-forms-item> -->
			<uni-forms-item label="昵称" required name="nickName">
				<uni-easyinput v-model="userinfo.nickName" placeholder="请输入昵称" />
			</uni-forms-item>
		</uni-forms>
		<button type="primary" @tap="tijiao('valiForm')">提 交</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				headpic: [],
				userinfo: {
					name: '',
					password: '',
					nickName: '',
					// headpic: ''
				},
				rules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '用户名不能为空'
						}]
					},
					password: {
						rules: [{
							required: true,
							errorMessage: '密码不能为空'
						}]
					},
					/* headpic: {
						rules: [{
							required: true,
							errorMessage: '头像不能为空'
						}]
					}, */
					nickName: {
						rules: [{
							required: true,
							errorMessage: '头像不能为空'
						}]
					}
				}
			};
		},
		methods: {
			// 获取上传状态
			select(e) {
				console.log('选择文件：', e)
				// wx.chooseMessageFile()
			},
			// 获取上传进度
			progress(e) {
				console.log('上传进度：', e)
			},

			// 上传成功
			success(e) {
				console.log('上传成功', e)
				this.userinfo.headpic = e.tempFiles[0].url
			},

			// 上传失败
			fail(e) {
				console.log('上传失败：', e)
			},
			//提交
			tijiao(ref) {
				uni.showLoading({
					title: 'loading',
					mask: true
				})
				console.log(this.userinfo)

				this.$refs[ref].validate().then(res => {
					console.log(res)
					uniCloud.callFunction({
						name: 'add-user',
						data: this.userinfo
					}).then(res => {
						console.log(res)
						uni.hideLoading()
						if (res.result.errCode == 1) {
							uni.showToast({
								title: res.result.msg,
								icon: "none",
								mask: true
							})
						} else {
							uni.switchTab({
								url: '/pages/my/my'
							})
						}


					})
				}).catch(err => {
					uni.hideLoading()
					console.log('err', err);
				})
			}
		}
	}
</script>

<style lang="scss">
	.con {
		display: flex;
		flex-direction: column;
		padding: 30rpx;
	}
</style>
